<?php
include 'securities.php';
class Users extends Securities {
	function __construct() {
		parent::__construct('users');
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
	}
	
	public function index(){
		
		$data['title']="Users";
		$data['controller_name']=strtolower(get_class());
		$data['manage_datatable']=get_users_manage_table($this->mod_globals->select_all(PREFIX_TABLE.'users'),$this->uri->segment(1));
		$this->load->view(B_MASTER.'index',$data);
	}
	public function insert_user(){
		$module_id=$this->uri->segment(1);
		if(!$this->is_has_modify_permission($module_id)){
	 			redirect('ctm-common/no-allow-modify/' . $module_id);
	 	}
		
		$data['title']="New User";
		$this->form_validation->set_rules('username','lang:form_label_username','required');
		$this->form_validation->set_rules('email','lang:form_label_email','required|valid_email');
		$this->form_validation->set_rules('first_name','lang:form_label_first_name','required');
		$this->form_validation->set_rules('last_name','lang:form_label_last_name','required');
		
		$this -> form_validation -> set_rules('password', 'lang:form_label_password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'lang:form_label_confirm_password', 'trim|min_length[4]|max_length[25]');
		
		
		if($this->form_validation->run()===FALSE){
			$data['dropdown_user_type']=get_dropdown_list_user_type($this->mod_globals->select_all(PREFIX_TABLE.'user_groups'));
			//$this->load->view('welcome_message',$data);
			$this->load->view(B_MASTER.'index',$data);
		}else{
			// Create users table
			$data_create_users=array(
				'use_user_group_id'=>$this->input->post('user_group'),
				'username'=>$this->input->post('username'),
				'first_name'=>$this->input->post('first_name'),
				'last_name'=>$this->input->post('last_name'),
				'email'=>$this->input->post('email'),
				'password'=>md5($this->input->post('password')),
				'status'=>$this->input->post('status'),
				'created_date'=>null
			);
			
			$is_saved=$this->mod_globals->insert(PREFIX_TABLE."users",$data_create_users);
			if($is_saved){
				$this->session->set_flashdata('success','User  was created success.');
				redirect('users');	
			}else{
				$this->session->set_flashdata('error','User  was created fail.');
				redirect('users');
			}
			
		}
	}

	public function update_user(){
		$module_id=$this->uri->segment(1);
		if(!$this->is_has_modify_permission($module_id)){
	 			redirect('ctm-common/no-allow-modify/' . $module_id);
	 	}
		
		$data['title']="Update User";
		$user_id=$this->uri->segment(3);
		$this->form_validation->set_rules('username','lang:form_label_username','required');
		$this->form_validation->set_rules('email','lang:form_label_email','required|valid_email');
		$this->form_validation->set_rules('first_name','lang:form_label_first_name','required');
		$this->form_validation->set_rules('last_name','lang:form_label_last_name','required');
		
		$this -> form_validation -> set_rules('password', 'lang:form_label_password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'lang:form_label_confirm_password', 'trim|min_length[4]|max_length[25]');
		
		if($this->form_validation->run()===FALSE){
			
			$data['user_data']=$this->mod_globals->select_where(PREFIX_TABLE."users",array('user_id'=>$user_id));
			$data['dropdown_user_type']=get_dropdown_list_user_type_select($this->mod_globals->select_all(PREFIX_TABLE.'user_groups'),$data['user_data']->row()->use_user_group_id);
			//$this->load->view('welcome_message',$data);
			$this->load->view(B_MASTER.'index',$data);
			
		}else{
			
			if($this->input->post('password')==""){
				
				$password=$this->mod_globals->select_string(PREFIX_TABLE."users", "password",array("user_id"=>$user_id));
			}else{
				
				$password=md5($this->input->post('password'));
			}
			// Create users table
			$data_update_users=array(
				'use_user_group_id'=>$this->input->post('user_group'),
				'username'=>$this->input->post('username'),
				'first_name'=>$this->input->post('first_name'),
				'last_name'=>$this->input->post('last_name'),
				'email'=>$this->input->post('email'),
				'password'=>$password,
				'status'=>$this->input->post('status')
			);
			
			$is_updated=$this->mod_globals->update(PREFIX_TABLE."users",$data_update_users,array('user_id'=>$user_id));
			if($is_updated){
				$this->session->set_flashdata('success','User data was updated success.');
				redirect('users');
			}else{
				$this->session->set_flashdata('error','User data was updated fail.');
				redirect('users');	
			}
			
		}
	}
	
	public function delete_user($user_id){
		$module_id=$this->uri->segment(1);
		if(!$this->is_has_modify_permission($module_id)){
	 			redirect('ctm-common/no-allow-modify/' . $module_id);
	 	}
		
		$whe_delete_user=array('user_id'=>$user_id);
		$is_deleted=$this->mod_globals->delete(PREFIX_TABLE.'users',$whe_delete_user);
		if($is_deleted){
			$this->session->set_flashdata('success','User data was deleted success.');
			redirect('users');
		}else{
			$this->session->set_flashdata('error','User data was deleted fail.');
			redirect('users');
		}
	}
	 
	 
}
